#include "stdafx.h"
#include "Audio.h"

namespace Reign
{namespace Audio
{namespace XAudio
{
	#pragma region Constructors
	Audio::Audio(DisposableI^ parent)
	: Disposable(parent)
	{
		null();
		
		try
		{
			IXAudio2* xAudioTEMP;
			uint flags = 0;
			#if DEBUG
			flags = XAUDIO2_DEBUG_ENGINE;
			#endif
			if(FAILED(XAudio2Create(&xAudioTEMP, flags)))
			{
				Debug::ThrowError(L"Audio", L"Could not create XAudio2 object");
			}
			xAudio = xAudioTEMP;

			IXAudio2MasteringVoice* masteringVoiceTEMP;
			if (FAILED(xAudio->CreateMasteringVoice(&masteringVoiceTEMP)))
			{
				Debug::ThrowError(L"Audio", L"Could not create MasteringVoice object");
			}
			masteringVoice = masteringVoiceTEMP;
		}
		catch (Exception^ e)
		{
			delete this;
			throw e;
		}
	}

	Audio::~Audio()
	{
		disposeChilderen();
		if (masteringVoice) masteringVoice->DestroyVoice();
		if (xAudio) xAudio->Release();
		null();
	}

	void Audio::null()
	{
		masteringVoice = 0;
		xAudio = 0;
	}
	#pragma endregion

	#pragma region Methods
	void Audio::Update()
	{
		if (UpdateCallback) UpdateCallback();
	}
	#pragma endregion
}
}
}